Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce Functions\stubWpUrlFunctions() helper #129

Open
wants to merge 8 commits into
base: master
Choose a base branch
from

Conversation

gmazzap
Copy link
Collaborator

@gmazzap gmazzap commented Dec 12, 2022

It makes use of a new UrlsHelper class.

Tests and documentation added.

See #125

It makes use of a new UrlsHelper class.

Tests and documentation added.

See #125
@gmazzap gmazzap requested a review from jrfnl December 12, 2022 18:23
Copy link
Collaborator

@jrfnl jrfnl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've not completely reviewed this PR yet as I believe this remark needs to be addressed/hashed out first:

It looks like most of these don't take passed parameters to the original function into account, while WP does support passing parameters.

Stubbing these in the framework without taking the passes parameters into account will probably be confusing and lead to support overhead or to people just not using these stubs as they don't do what they expect.

As addressing that will require some rework in the PR (if it will be addressed), I figured my time would be better spend reviewing after the discussion has been had about the above.

docs/functions-testing-tools/function-stubs.md Outdated Show resolved Hide resolved
docs/functions-testing-tools/function-stubs.md Outdated Show resolved Hide resolved
inc/api.php Show resolved Hide resolved
inc/api.php Outdated Show resolved Hide resolved
src/Expectation/UrlsHelper.php Outdated Show resolved Hide resolved
@@ -0,0 +1,125 @@
<?php
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All @param declarations in this file are missing the type information, which makes them kind of useless.

Copy link
Collaborator Author

@gmazzap gmazzap Apr 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All @param are mixed anyway. The type is checked inside the function via is_string and similar. So you can pass whatever. Mixed is implicit so to me no explicit type would be fine.

If we would add Psalm or similar, as soon I would declare @param string and then do is_string() checks inside the function Psalm would rightfully complain for "redundant condition" and "doc bloc type contradiction" (see an example).

I prefer to keep the check at runtime so all the parameters are mixed, and IMO there's no point in typing "mixed" all over the place.
Still, I like to keep the doc block for "visual help". I personally find it hard to digest a wall of code without some "calming" doc block in between functions.

Anyway, I added mixed pretty much everywhere, unless a private method where the type of the parameters is ensured.

Comment on lines +195 to +209
'home_url' => $helper->stubUrlCallback(),
'get_home_url' => $helper->stubUrlForSiteCallback(),
'site_url' => $helper->stubUrlCallback(),
'get_site_url' => $helper->stubUrlForSiteCallback(),
'admin_url' => $helper->stubUrlCallback('wp-admin', 'admin'),
'get_admin_url' => $helper->stubUrlForSiteCallback('wp-admin', 'admin'),
'content_url' => $helper->stubUrlCallback('wp-content', null, false),
'rest_url' => $helper->stubUrlCallback('wp-json'),
'get_rest_url' => $helper->stubUrlForSiteCallback('wp-json'),
'includes_url' => $helper->stubUrlCallback('wp-includes'),
'network_home_url' => $helper->stubUrlCallback(),
'network_site_url' => $helper->stubUrlCallback(),
'network_admin_url' => $helper->stubUrlCallback('wp-admin/network', 'admin'),
'user_admin_url' => $helper->stubUrlCallback('wp-admin/user', 'admin'),
'wp_login_url' => static function ($redirect = '', $force_reauth = false) use ($helper) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like most of these don't take passed parameters to the original function into account, while WP does support passing parameters.

Stubbing these in the framework without taking the passes parameters into account will probably be confusing and lead to support overhead or to people just not using these stubs as they don't do what they expect.

Copy link
Collaborator Author

@gmazzap gmazzap Apr 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jrfnl I think you need to look at it again :)

$helper->stubUrlCallback() and $helper->stubUrlForSiteCallback() return callbacks that take respectively 2 ($path, $schema) and 3 parameters ($blog_id, $path, $schema), that are the same core functions take.

There are 2 exceptions: content_url() and wp_login_url().

wp_login_url() takes still 2 arguments, but different from the others, and that is why it is stubbed differently.

content_url() takes 1 parameter: $path. That is why stubUrlCallback() accepts a 3rd parameter $use_schema_arg that defaults to true, but it is passed as false for content_url().

To be noted:

  • the $blog_id parameter for functions that accepts it is just ignored in stubbed functions because the base URL is taken from what is passed to the stubWpUrlFunctions() API function.
  • When $schema param is something like 'admin' or 'login' it is taken into account only if HTTPs is not forced (passing true as stubWpUrlFunctions()'s 2nd parameter) and force_ssl_admin() is available (likely because stubbed separately).
  • rest_url() and get_rest_url() use 'rest' as scheme. That is not relevant for the generation of the URL (in WP just like in the stubbed version), it is used in WP only as the 3rd parameter, $orig_scheme, passed to the 'set_url_scheme' hook, something that I think is pretty safe to ignore for the stubbed version of the function.

TL;DR: all the stubbed functions accept the same parameters and work very similarly to the WP counterparts. For some edge cases, it might be needed to stub is_ssl() and/or force_ssl_admin() separately.

You can even see the parameters used with success in tests: https://github.com/Brain-WP/BrainMonkey/blob/feature/stub-wp-urls/tests/cases/unit/Api/FunctionsTest.php#L439-L455

@codecov-commenter
Copy link

codecov-commenter commented Apr 19, 2023

Codecov Report

Patch coverage: 97.91% and project coverage change: -0.02 ⚠️

Comparison is base (d9ff201) 92.25% compared to head (dfffa27) 92.23%.

📣 This organization is not using Codecov’s GitHub App Integration. We recommend you install it so Codecov can continue to function properly for your repositories. Learn more

Additional details and impacted files
@@             Coverage Diff              @@
##             master     #129      +/-   ##
============================================
- Coverage     92.25%   92.23%   -0.02%     
- Complexity      312      342      +30     
============================================
  Files            28       29       +1     
  Lines           710      876     +166     
============================================
+ Hits            655      808     +153     
- Misses           55       68      +13     
Impacted Files Coverage Δ
src/Expectation/UrlsHelper.php 97.91% <97.91%> (ø)

... and 20 files with indirect coverage changes

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report in Codecov by Sentry.
📢 Do you have feedback about the report comment? Let us know in this issue.

@gmazzap gmazzap added this to the 2.x Next milestone Apr 19, 2023
@gmazzap gmazzap requested a review from jrfnl April 19, 2023 09:23
@gmazzap
Copy link
Collaborator Author

gmazzap commented Apr 19, 2023

I've not completely reviewed this PR yet as I believe this remark needs to be addressed/hashed out first:

It looks like most of these don't take passed parameters to the original function into account, while WP does support passing parameters.
Stubbing these in the framework without taking the passes parameters into account will probably be confusing and lead to support overhead or to people just not using these stubs as they don't do what they expect.

As addressing that will require some rework in the PR (if it will be addressed), I figured my time would be better spend reviewing after the discussion has been had about the above.

Commented all your points.

Your concerns about parameters, as better explained below, aren't correct because parameters are accepted in the same way WP does. Besides my reasoning in other comments, there're tests to prove it :)

So, please @jrfnl, review it again when/if you have time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants